package offerv1;

public class Offer52 {

    //hash表
    //双指针
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode aIndex = headA, bIndex = headB;
        while (aIndex != null || bIndex != null) {
            aIndex = aIndex != null ? aIndex : headB;
            bIndex = bIndex != null ? bIndex : headA;

            if (aIndex == bIndex) {
                return aIndex;
            }

            aIndex = aIndex.next;
            bIndex = bIndex.next;
        }

        return null;
    }

    public ListNode getIntersectionNode1(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode pA = headA, pB = headB;
        while (pA != pB) {
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }
        return pA;
    }
}
